package cn.qdhlm.collectservice.core.component.analyse;

import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.util.CodeUtil;


import cn.qdhlm.collectservice.service.UtilityService;
import io.netty.buffer.ByteBuf;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Name: AnyAfn03
 * @Description: 运行参数（AFN=03H）
 * @Author: jiangcs
 */
@Log4j2
@Component
public class AnyAfn03 {

    @Autowired
    private UtilityService utilityService;

    public boolean execute(int da, int dt, String collectorNo, ByteBuf byteBuf) throws Exception {
        // F5：设备主动发送ICCID
        if (da == Constant.DA_0 && dt == Constant.DT_4096) {
            // iccid
            StringBuilder sbIccid = new StringBuilder();
            for (int i = 0; i < Constant.BYTE_LENGTH_ICCID; i++) {
                String asciiStr = CodeUtil.byteToHexString(byteBuf.readByte());
                if (Constant.SYMBOL_00.equals(asciiStr)) {
                    // 跳过剩余字节
                    byteBuf.skipBytes(Constant.BYTE_LENGTH_ICCID - i - 1);
                    break;
                }
                sbIccid.append(CodeUtil.hexStringToAsciiString(asciiStr));
            }

            // network
            int network = byteBuf.readByte();

            // signal
            int signal = byteBuf.readByte();

            // 更新 采集器SIM卡信息
//            int count = collectorMapper.updateSimNumber(collectorNo.getId(), sbIccid.toString());
//            log.info("update collectorId[{}]: simNumber[{}] => count[{}]", collectorNo.getId(), sbIccid, count);

            // 采集器网络信息存储至mongodb
            utilityService.sendOnlineData(Constant.COLLECTOR_ONLINE_TYPE_OPERATE_PARAM, collectorNo, null, network, signal);

            return true;
        }

        log.error("execute: da[{}], dt[{}] match fail", da, dt);
        return false;
    }
}
